import java.awt.Color;
import java.awt.GraphicsConfiguration;
import java.awt.HeadlessException;
import java.awt.Rectangle;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;

/**
 * Create a frame and a histogram panel in the frame
 *
 */

public class HistogramFrame extends JFrame {

	private static final long serialVersionUID = 1L;
	/**
	 * histogram panel
	 */
	private histogram jContentPane=null;
	private JScrollPane pan;
	/**
	 * num - numbers of bars
	 */
	int num;
	/**
	 * width - width of each bar 
	 */
	int width;
	/**
	 * sp - sp between bars 
	 */
	int sp;
	/**
	 * bar - color for the first type of bars
	*/
	Color bar;
	/**
	* bar1 - color for the second type of bars
	 */
	Color bar1;
	/**
	 * bar_size - sizes of the bars from the first type
	*/
	int[] bar_size;
	/**
	 * bar_size1 - sizes of the bars from the second type 
	 */
	int[] bar_size1;
	/**
	 * bar_name - Array of strings the name of each bar
	 */
	String[] bar_name;
	/**
	 * xname - Title for x axis
	 */
	String xname;
	/**
	 * yname - Title for y axis 
	 */
	String yname;
	/**
	 * title - panel title 
	 */
	String title;
	/**
	  * bar_title - The name of the first bars type 
	 */
	String bar_title;
	/**
	  * bar_title1 - The name of the second bars type 
	 */
	String bar_title1;
	/**
	 * panel_width - panel width
	 */
	int panel_width;
	/**
	 * panel_hight - panel hight 
	 */
	int panel_hight;
	int i;
	/**
	 * Create and initializes HistogramFrame
	 * @param bar_number Number of bars.
	 * @param bar_width Width of each bar.
	 * @param bar_space Space between bars.
	 * @param bar_c The first bars type color.
	 * @param bar_c1 The second bars type color.
	 * @param bar_height The first bars type sizes.
	 * @param bar_height1 The second bars type sizes.
	 * @param bar_name1 Names for all bars.
	 * @param y_name Title for y axis
	 * @param x_name Title for x axis
	 * @param frame_title Title for the frame
	 * @param bar_title_tmp The first bars type title.
	 * @param bar_title_tmp1 The second bars type title.
	 * @throws HeadlessException
	 */
	public HistogramFrame(int bar_number,int bar_width,int bar_space,Color bar_c,Color bar_c1,int[] bar_height,int[] bar_height1,String[] bar_name1,String y_name,String x_name,String frame_title,String bar_title_tmp,String bar_title_tmp1) throws HeadlessException {
		// TODO Auto-generated constructor stub
		super();
		num=bar_number;
		width=bar_width;
		if(width%2!=0)
			width++;
		sp=bar_space;
		bar=bar_c;
		bar1=bar_c1;
		bar_size=bar_height;
		bar_size1=bar_height1;
		xname=x_name;
		yname=y_name;
		title=frame_title;
		bar_name=bar_name1;
		bar_title=bar_title_tmp;
		bar_title1=bar_title_tmp1;
		panel_width=25+5+3+5+histogram.labelSize(yname)+histogram.labelSize(xname);
		if(panel_width>700)
			panel_width=700;
		if(panel_width<300)
			panel_width=300;
		initialize();
	}

	public HistogramFrame(GraphicsConfiguration arg0) {
		super(arg0);
		// TODO Auto-generated constructor stub
		initialize();
	}

	public HistogramFrame(String arg0) throws HeadlessException {
		super(arg0);
		// TODO Auto-generated constructor stub
		initialize();
	}

	public HistogramFrame(String arg0, GraphicsConfiguration arg1) {
		super(arg0, arg1);
		// TODO Auto-generated constructor stub
		initialize();
	}

	/**
	 *initializes this
	 * 
	 * @return void
	 */
	private void initialize() {
		for(i=0;i<num;i++)
		{
			int x_move;
			if(histogram.labelSize(bar_name[i])>2*width)
			{
				x_move=histogram.labelSize(bar_name[i]);
			}
			else
			{
				x_move=2*width;
			}
			panel_width=panel_width+x_move+sp;
		}
		panel_hight=233;
		this.setSize(panel_width+100, panel_hight+200);
		pan=new JScrollPane();
		pan.setLayout(null);
		pan.setBounds(new Rectangle(33, 56, 276, 177));
		pan.setViewportView(getJContentPane());
		this.setContentPane(jContentPane);
		this.setTitle("Histogram");
		
	}

	/**
	 *initializes jContentPane and set btnClose to close the frame
	 * 
	 * @return JPanel
	 */
	private JPanel getJContentPane() {
		if (jContentPane == null) {
			jContentPane = new histogram(num,width,sp,bar,bar1,bar_size,bar_size1,bar_name,yname,xname,title,panel_width,panel_hight,bar_title,bar_title1);
			jContentPane.setLayout(null);
			jContentPane.btnClose.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					setVisible(false);
				}
			});
			
		}
		return jContentPane;
	}

}
